/** * 基础菜单 商品管理 */
<template>
	<div>
		<!-- 面包屑导航 -->
		<el-breadcrumb separator-class="el-icon-arrow-right">
			<el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
			<el-breadcrumb-item>所有技师</el-breadcrumb-item>
		</el-breadcrumb>
		<!-- 搜索筛选 -->
		<el-form :inline="true" :model="formInline" class="user-search">
			<el-form-item label="搜索："><el-input size="small" v-model="formInline.deptName" placeholder="输入技师名称"></el-input></el-form-item>
			<el-form-item label=""><el-input size="small" v-model="formInline.deptNo" placeholder="输入技师平台"></el-input></el-form-item>
			<el-form-item>
				<el-button size="small" type="primary" icon="el-icon-search" @click="search">搜索</el-button>
				<el-button size="small" type="primary" icon="el-icon-plus" @click="handleEdit()">添加</el-button>
			</el-form-item>
		</el-form>
		<!--列表-->
		<el-table size="small" :data="listData" highlight-current-row v-loading="loading" border element-loading-text="拼命加载中" style="width: 100%;">
			<el-table-column align="center" type="selection" width="60"></el-table-column>
			<el-table-column sortable prop="HeadImg" label="技师头像" width="200"></el-table-column>
			<el-table-column sortable prop="name" label="技师名称" width="200"></el-table-column>
			<el-table-column sortable prop="deptNo" label="技师平台" width="200"></el-table-column>
			<el-table-column sortable prop="createtime" label="加入时间" width="200">
				<!-- <template slot-scope="scope">
					<div>{{scope.row.editTime|timestampToTime}}</div>
				</template> -->
			</el-table-column>
			<el-table-column align="center" label="操作" min-width="300">
				<template slot-scope="scope">
					<el-button size="mini" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
					<el-button size="mini" type="danger" @click="deleteUser(scope.$index, scope.row)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>
		<!-- 分页组件 -->
		<Pagination v-bind:child-msg="pageparm" @callFather="callFather"></Pagination>
		<!-- 编辑界面 -->
		<el-dialog class="poup" :title="title" :visible.sync="editFormVisible" width="30%" @click="closeDialog">
			<el-form label-width="120px" :model="editForm" :rules="rules" ref="editForm">
				<el-form-item label="手机号" prop="phone">
					<el-input size="small" type="phone" maxlength="11" v-model.number="editForm.phone" auto-complete="off" placeholder="请输入手机号"></el-input>
				</el-form-item>
				<el-form-item label="姓名" prop="name"><el-input size="small" v-model="editForm.name" auto-complete="off" placeholder="请输入姓名"></el-input></el-form-item>
				<el-form-item label="身份证" prop="IdCard">
					<el-input size="small" type="phone" v-model.number="editForm.IdCard" auto-complete="off" placeholder="请输入身份证"></el-input>
				</el-form-item>
				<el-form-item label="身份证正面照片" prop="positiveimg">
					<el-upload class="avatar-uploader" action="#" :show-file-list="false" :http-request="upload" :before-upload="ImgBeforeAvatarUpload">
						<img v-if="editForm.positiveimg" :src="editForm.positiveimg" class="avatar" />
						<i v-else class="el-icon-plus avatar-uploader-icon"></i>
					</el-upload>
				</el-form-item>
				<el-form-item label="身份证背面照片" prop="reverseimg">
					<el-upload class="avatar-uploader" action="#1" :show-file-list="false" :http-request="upload" :before-upload="ImgBeforeAvatarUpload">
						<img v-if="editForm.reverseimg" :src="editForm.reverseimg" class="avatar" />
						<i v-else class="el-icon-plus avatar-uploader-icon"></i>
					</el-upload>
				</el-form-item>
				<el-form-item label="资质证照片" prop="qualificationimg">
					<el-upload class="avatar-uploader" action="#2" :show-file-list="false" :http-request="upload" :before-upload="ImgBeforeAvatarUpload">
						<img v-if="editForm.qualificationimg" :src="editForm.qualificationimg" class="avatar" />
						<i v-else class="el-icon-plus avatar-uploader-icon"></i>
					</el-upload>
				</el-form-item>
				<el-form-item label="分成比(%)" prop="dividedInto"><el-input size="small" type="number" maxlength="2" v-model="editForm.dividedInto" auto-complete="off" placeholder="请输入分成比"></el-input></el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button size="small" @click="closeDialog">取消</el-button>
				<el-button size="small" type="primary" :loading="loading" class="title" @click="submitForm('editForm')">保存</el-button>
			</div>
		</el-dialog>
	</div>
</template>

<script>
import { deptList, deptSave, deptDelete, uploadImg } from '../../api/userMG';
import Pagination from '../../components/Pagination';
export default {
	data() {
		return {
			nshow: true, //switch开启
			fshow: false, //switch关闭
			loading: false, //是显示加载
			editFormVisible: false, //控制编辑页面显示与隐藏
			title: '添加',
			editForm: {
				id:'',
				phone: '',
				name: '',
				IdCard: '',
				positiveimg: '',
				reverseimg: '',
				qualificationimg: '',
				dividedInto: ''
			},
			// rules表单验证
			rules: {
				phone: [{ required: true, message: '请输入手机号', trigger: 'blur' }],
				name: [{ required: true, message: '请输入技师姓名', trigger: 'blur' }],
				IdCard: [{ required: true, message: '请输入身份证', trigger: 'blur' }],
				positiveimg: [{ required: true, message: '请选择身份证正面照片', trigger: 'blur' }],
				reverseimg: [{ required: true, message: '请选择身份证反面照片', trigger: 'blur' }],
				qualificationimg: [{ required: true, message: '请选择资质证照片', trigger: 'blur' }],
				dividedInto: [{ required: true, message: '请输入分成比', trigger: 'blur' }]
			},
			formInline: {
				page: 1,
				pagenum: 10
			},
			// 删除部门
			seletedata: {
				ids: ''
			},
			userparm: [], //搜索权限
			listData: [], //用户数据
			// 分页参数
			pageparm: {
				currentPage: 1,
				pageSize: 10,
				total: 10
			},
			imageUrl: '',
			imageUrl1: '',
			imageUrl2: '',
		};
	},
	// 注册组件
	components: {
		Pagination
	},
	/**
	 * 数据发生改变
	 */

	/**
	 * 创建完毕
	 */
	created() {
		this.getdata(this.formInline);
	},

	/**
	 * 里面的方法只有被调用才会执行
	 */
	methods: {
		upload(f) {
			// console.log(f)
			// 1. 获取表单数据   fromData 表单数据
			let fromData = new FormData();
			fromData.append('file', f.file);
			// 2. 发起请求
			uploadImg(fromData).then(res => {
				//3.请求成功之后  图片回显
				if(f.action=='#'){
					this.editForm.positiveimg = res.url;
				}else if(f.action=='#1'){
					this.editForm.reverseimg = res.url;
				}else if(f.action=='#2'){
					this.editForm.qualificationimg = res.url;
				}
			}).catch(err => {
				this.$message.error('上传图片失败');
			});
		},
		//上传前校验图片规则
		ImgBeforeAvatarUpload(file) {
			// console.log(file);
			const isJPG = file.type === 'image/jpeg' || file.type === 'image/png';
			const isLt2M = file.size / 1024 / 1024 < 2;
			if (!isJPG) {
				this.$message.error('上传图片只能是 JPG 和 Png 格式!');
				return false;
			}
			if (!isLt2M) {
				this.$message.error('上传图片大小不能超过 2MB!');
				return false;
			}
		},
		// 获取公司列表
		getdata(parameter) {
			parameter.adminstratorID = JSON.parse(localStorage.getItem('userdata')).ID;
			this.loading = true;

			/***
			 * 调用接口，注释上面模拟数据 取消下面注释
			 */

			deptList(parameter).then(res => {
				// console.log(res)
				this.loading = false;
				this.$message({
					type: 'info',
					message: res.message
				});
				this.listData = res.date;
				// 分页赋值
				// this.pageparm.currentPage = this.formInline.page;
				// this.pageparm.pageSize = this.formInline.pagenum;
				this.pageparm.total = res.count
			}).catch(err => {
				// console.log(err)
				this.loading = false;
				this.$message.error('菜单加载失败，请稍后再试！');
			});
		},
		// 分页插件事件
		callFather(parm) {
			this.formInline.page = parm.currentPage;
			this.formInline.limit = parm.pageSize;
			this.getdata(this.formInline);
		},
		// 搜索事件
		search() {
			this.getdata(this.formInline);
		},
		//显示编辑界面
		handleEdit: function(index, row) {
			this.editFormVisible = true;
			if (row != undefined && row != 'undefined') {
				this.title = '修改';
				// this.editForm.id = row.id;
				// this.editForm.phone = row.phone;
				// this.editForm.name = row.name;
				// this.editForm.IdCard = row.IdCard;
				// this.editForm.positiveimg = row.positiveimg;
				// this.editForm.reverseimg = row.reverseimg;
				// this.editForm.qualificationimg = row.qualificationimg;
				// this.editForm.dividedInto = row.dividedInto;
			} else {
				this.title = '添加';
				this.editForm.id = '';
				this.editForm.phone = '';
				this.editForm.name = '';
				this.editForm.IdCard = '';
				this.editForm.positiveimg = '';
				this.editForm.reverseimg = '';
				this.editForm.qualificationimg = '';
				this.editForm.dividedInto = '';
			}
		},
		// 编辑、增加页面保存方法
		submitForm(editData) {
			this.$refs[editData].validate(valid => {
				if (valid) {
					deptSave(this.editForm).then(res => {
						this.editFormVisible = false;
						this.loading = false;
						this.getdata(this.formInline);
						this.$message({
							type: 'success',
							message: '技师保存成功！'
						});
					}).catch(err => {
						this.editFormVisible = false;
						this.loading = false;
						this.$message.error('技师保存失败，请稍后再试！');
					});
				} else {
					return false;
				}
			});
		},
		// 删除技师
		deleteUser(index, row) {
			this.$confirm('确定要删除吗?', '信息', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning'
			}).then(() => {
				deptDelete({uuid:row.uuid}).then(res => {
					this.$message({
						type: 'success',
						message: '技师已删除!'
					});
					this.getdata(this.formInline);
				}).catch(err => {
					this.loading = false;
					this.$message.error('技师删除失败，请稍后再试！');
				});
			}).catch(() => {
				this.$message({
					type: 'info',
					message: '已取消删除'
				});
			});
		},
		// 关闭编辑、增加弹出框
		closeDialog() {
			this.editFormVisible = false;
		}
	}
};
</script>

<style scoped lang="less">
	.poup{
		overflow: auto;
	}
.avatar-uploader {
	.el-upload {
		border: 1px dashed #8c939d;
		border-radius: 6px;
		cursor: pointer;
		position: relative;
		overflow: hidden;
	}
	.el-upload:hover {
		border-color: #409eff;
	}
}
.avatar-uploader-icon {
	border: 1px dashed #8c939d;
	font-size: 28px;
	color: #8c939d;
	width: 80px;
	height: 80px;
	line-height: 80px;
	text-align: center;
}
.avatar {
	width: 80px;
	height: 80px;
	display: block;
}
.user-search {
	margin-top: 20px;
}
.userRole {
	width: 100%;
}
</style>
